package net.dopan.pigframe.user.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.BaseController;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.core.utils.Query;
import net.dopan.pigframe.user.model.entity.SysDict;
import net.dopan.pigframe.user.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Package: net.dopan.pigframe.user.controller
 * @Title: DictController
 * @Description: 字典表
 * @author: 刘宽
 * @date: 2018/9/2 21:27
 */
@RestController
@RequestMapping("/dict")
@Api(tags = "字典表接口(API)", description = "字典表相关操作")
public class DictController extends BaseController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 通过ID查询字典信息
     *
     * @param id ID
     * @return 字典信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询字典信息", notes = "通过ID查询字典信息")
    public SysDict dict(@PathVariable Integer id) {
        return sysDictService.selectById(id);
    }

    /**
     * 分页查询字典信息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @RequestMapping("/dictPage")
    @ApiOperation(value = "分页查询字典信息", notes = "分页查询字典信息")
    public Page dictPage(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return sysDictService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 通过字典类型查找字典
     *
     * @param type 类型
     * @return 同类型字典
     */
    @GetMapping("/type/{type}")
    @ApiOperation(value = "通过字典类型查找字典", notes = "通过字典类型查找字典")
    @Cacheable(value = "dict_details", key = "#type")
    public List<SysDict> findDictByType(@PathVariable String type) {
        SysDict condition = new SysDict();
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        condition.setType(type);
        return sysDictService.selectList(new EntityWrapper<>(condition));
    }

    /**
     * 添加字典
     *
     * @param sysDict 字典信息
     * @return success、false
     */
    @PostMapping
    @ApiOperation(value = "添加字典", notes = "添加字典")
    @CacheEvict(value = "dict_details", key = "#sysDict.type")
    public ResponseBean<Boolean> dict(@RequestBody SysDict sysDict) {
        return new ResponseBean<>(sysDictService.insert(sysDict));
    }

    /**
     * 删除字典，并且清除字典缓存
     *
     * @param id   ID
     * @param type 类型
     * @return R
     */
    @DeleteMapping("/{id}/{type}")
    @ApiOperation(value = "删除字典，并且清除字典缓存", notes = "删除字典，并且清除字典缓存")
    @CacheEvict(value = "dict_details", key = "#type")
    public ResponseBean<Boolean> deleteDict(@PathVariable Integer id, @PathVariable String type) {
        return new ResponseBean<>(sysDictService.deleteById(id));
    }

    /**
     * 修改字典
     *
     * @param sysDict 字典信息
     * @return success/false
     */
    @PutMapping
    @ApiOperation(value = "修改字典", notes = "修改字典")
    @CacheEvict(value = "dict_details", key = "#sysDict.type")
    public ResponseBean<Boolean> editDict(@RequestBody SysDict sysDict) {
        return new ResponseBean<>(sysDictService.updateById(sysDict));
    }
}
